package admin.server.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @author zgb
 * @date 2024-02-23 17:03
 * @description TODO
 */
@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes!=null){
            HttpServletRequest request = attributes.getRequest();
            //添加token
            String header = request.getHeader(HttpHeaders.AUTHORIZATION);
            Collection<String> headers = template.request().headers().get(HttpHeaders.AUTHORIZATION);
            if((headers==null|| headers.isEmpty())&&header!=null){
                template.header(HttpHeaders.AUTHORIZATION, header);
            }
        }
    }




}
